<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="原价" prop="originalPrice">
        <el-input v-model="queryParams.originalPrice" placeholder="请输入原价" clearable @keyup.enter="handleQuery" style="width:200px;"/>
      </el-form-item>
      <el-form-item label="折扣价格" prop="presentPrice">
        <el-input v-model="queryParams.presentPrice" placeholder="请输入折扣价格" clearable @keyup.enter="handleQuery" style="width:200px;"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['chy:servicedetails:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['chy:servicedetails:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['chy:servicedetails:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['chy:servicedetails:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="servicedetailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="服务项目" align="center" prop="name" />
      <el-table-column label="商家名称" align="center" prop="mId">
        <template #default="scope">
          <span v-for="m in merchantList" :key="m.id">
            <span v-if="m.id === scope.row.mId">
              {{ m.name }}
            </span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="服务类别" align="center" prop="typeId">
        <template #default="scope">
          <span v-for="t in serviceTypeList" :key="t.id">
            <span v-if="t.id === scope.row.typeId">
              {{ t.name }}
            </span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="原价(元)" align="center" prop="originalPrice" />
      <el-table-column label="折扣价(元)" align="center" prop="presentPrice" />
      <el-table-column label="服务介绍" align="center" prop="introduction" />
      <el-table-column label="服务照片" align="center" prop="img" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['chy:servicedetails:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['chy:servicedetails:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改服务详情对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <!-- 服务项目 -->
      <el-form-item label="服务项目" prop="name">
        <el-input v-model="form.name" placeholder="请输入服务项目" />
      </el-form-item>
      <!-- 商家名称使用下拉框 -->
      <el-form-item label="商家名称" prop="mId">
        <el-select v-model="form.mId" placeholder="请选择商家名称">
          <el-option v-for="item in merchantList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <!-- 服务类别使用下拉框 -->
      <el-form-item label="服务类别" prop="typeId">
        <el-select v-model="form.typeId" placeholder="请选择服务类别">
          <el-option v-for="item in serviceTypeList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form ref="servicedetailsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="原价" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="折扣价格" prop="presentPrice">
          <el-input v-model="form.presentPrice" placeholder="请输入折扣价格" />
        </el-form-item>
        <el-form-item label="服务介绍" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入服务介绍" />
        </el-form-item>
        <el-form-item label="服务照片" prop="img">
          <image-upload v-model="form.img" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Servicedetails">
import { listServicedetails, getServicedetails, delServicedetails, addServicedetails, updateServicedetails } from "@/api/chy/servicedetails"
import { listMerchant } from "@/api/chy/merchant" // 导入商家名称列表接口
import { listService_type } from "@/api/chy/service_type" // 导入服务类别列表接口

const { proxy } = getCurrentInstance()

const servicedetailsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const merchantList = ref([]) // 商家名称列表
const serviceTypeList = ref([]) // 服务类别列表

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    mId: null,
    typeId: null,
    originalPrice: null,
    presentPrice: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询服务详情列表 */
function getList() {
  loading.value = true
  listServicedetails(queryParams.value).then(response => {
    servicedetailsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
  // 加载商家名称列表
  listMerchant().then(response => {
    merchantList.value = response.rows
    // console.log(merchantList.value)
  })
  // 加载服务类别列表
  listService_type().then(response => {
    serviceTypeList.value = response.rows
    // console.log(serviceTypeList.value)
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    mId: null,
    typeId: null,
    originalPrice: null,
    presentPrice: null,
    introduction: null,
    img: null,
    del: null
  }
  proxy.resetForm("servicedetailsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加服务详情"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getServicedetails(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改服务详情"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["servicedetailsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateServicedetails(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addServicedetails(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除服务详情编号为"' + _ids + '"的数据项？').then(function () {
    return delServicedetails(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('chy/servicedetails/export', {
    ...queryParams.value
  }, `servicedetails_${new Date().getTime()}.xlsx`)
}

getList()
</script>
